//
//  OneDoVC.swift
//  DollBuy
//
//  Created by 郑东喜 on 2016/11/22.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit
import WebKit


class OneDoVC : BaseViewController,WKUIDelegate,WKScriptMessageHandler,WKNavigationDelegate {
    
    
    var webView: WKWebView!
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        self.navigationItem.title = "提现"
        
        var webView = WKWebView()
        
        //wkwebview相关配置
        

        let configuration = WKWebViewConfiguration()
        let userContentController = WKUserContentController()
        
        //添加交互条目
        userContentController.add(self as WKScriptMessageHandler, name: "weixinPay")
        
        //跳转
        userContentController.add(self as WKScriptMessageHandler, name: "logout")
        
        configuration.userContentController = userContentController
        
        let preferences = WKPreferences()
        preferences.javaScriptCanOpenWindowsAutomatically = true
        preferences.minimumFontSize = 10.0
        configuration.preferences = preferences
        
        webView.uiDelegate = self
        
        let rect = CGRect(x: 0, y: 35, width: SW, height: SH - 64 - 84)
        webView = WKWebView.init(frame: rect, configuration: configuration)
        
        //本地加载
        let htmlPath = Bundle.main.path(forResource: "WKWebViewMessageHandler", ofType: "html")

        DispatchQueue.main.async {
            do {
                let fileURL = try NSString.init(contentsOfFile: htmlPath!, encoding: String.Encoding.utf8.rawValue)
                let baseURL = NSURL.fileURL(withPath: htmlPath! as String)
                webView.loadHTMLString(fileURL as String, baseURL: baseURL)
            } catch  {
                
            }
            
        }

        view.addSubview(webView)
        
    }
    
    /**
     *  JS 调用 swift 时 webview 会调用此方法
     *
     *  @param userContentController  webview中配置的userContentController 信息
     *  @param message                JS执行传递的消息
     */
    
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        
        print("\((#file as NSString).lastPathComponent):(\(#line))")
        
        let msg = message.name
//
//        //将获取到的数据解析成字典
//        let messageDic = message.body as! NSDictionary
//        
//        print(messageDic)
        
        if msg == "logout" {
            print("qq share")

            print(message.body)

        } else if msg == "qqshare" {
            
            
            print(message.body)
            
            let dic = message.body as! NSDictionary
            print(dic["shoppinr"])
            
            let dicStr = ((message.body) as! String).data(using: .utf8)
            
            do
            {
                let daaaa = try JSONSerialization.jsonObject(with: dicStr! as Data, options: JSONSerialization.ReadingOptions.allowFragments) as! NSDictionary
                print(daaaa)

            } catch {
                
            }

        }
    
    }

    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        //  NSString *hostname = navigationAction.request.URL.host.lowercaseString;
        let hostname = navigationAction.request.url?.host?.lowercased()
        print(hostname)
    }


}


